package com.awesome.uidemo.customview.customview.base;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.awesome.common.util.log.ShowLogUtil;

/**
 * Author: zhangjingfang
 * Email:zhangjingfang@jeejio.com
 * Date: 2022/3/21 10:08 下午
 * Description:类描述
 */
public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 获取自身宽高
        int width = getRight() - getLeft();
        int height = getBottom() - getTop();
        // 获取自身宽高
        int width2 = getWidth();
        int height2 = getHeight();
        ShowLogUtil.info("width="+width);
        ShowLogUtil.info("height="+height);
        ShowLogUtil.info("width2="+width2);
        ShowLogUtil.info("height2="+height2);
        // View自身的坐标
        // 自身顶边到父布局顶边的距离
        int top = getTop();
        // 自身左边到父布局左边的距离
        int left = getLeft();
        // 自身底边到父布局底边的距离
        int bottom = getBottom();
        // 自身右边到父布局右边的距离
        int right = getRight();
        ShowLogUtil.info("top="+top);
        ShowLogUtil.info("left="+left);
        ShowLogUtil.info("bottom="+bottom);
        ShowLogUtil.info("right="+right);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            // 点击时距控件左边的距离。视图坐标
            float x = event.getX();
            // 点击时距控件右边的距离。视图坐标
            float y = event.getY();
            // 点击时距整个屏幕左边的距离。绝对坐标
            float rawX = event.getRawX();
            // 点击时距整个屏幕右边的距离。绝对坐标
            float rawY = event.getRawY();
            ShowLogUtil.info("x="+x);
            ShowLogUtil.info("y="+y);
            ShowLogUtil.info("rawX="+rawX);
            ShowLogUtil.info("rawY="+rawY);
        }

        return super.onTouchEvent(event);
    }
}